<?php

	session_start();
	
	// check for login
	if(!isset($_SESSION['atbat']) && !isset($_COOKIE['remember_atbat'])) {
	
		// no login, move to index page
		header("Location: http://localhost/atbat/html/");
	
	}
	
	// connect to db
	require_once '../db/db.php';
	
	// get user id
	$sql = "SELECT user_id FROM users WHERE MD5(username) IN ('";
	$sql .= ((isset($_SESSION['atbat'])) ? $_SESSION['atbat'] : '') . "', '";
	$sql .= ((isset($_COOKIE['remember_atbat'])) ? $_COOKIE['remember_atbat'] : '') . "')";
		
	$id = $db->query($sql)->fetchAll();
	$user_id = $id[0]['user_id'];

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="http://localhost/atbat/html/" />
	<meta charset="utf-8">
	<title>AtBat :: Teams</title>
	<meta name="description" content="">
	<meta name="author" content="Sean Mullin, http://parametercontraption.com">
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
	<link rel="shortcut icon" href="images/design/favicon.ico">
	<link rel="apple-touch-icon" href="images/design/apple-touch-icon.png">
	<link rel="stylesheet" href="css/screen.css"><link rel="stylesheet" media="all" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css">
    <script src="js/modernizr-1.5.min.js"></script>
</head>
<body>
	<?php require_once 'include/header.html'; ?>
    <section id="content">
    	<h1>Teams</h1>
        <p><a href="create-team">Create a New Team</a>.</p>
        <article>
        	<table cellpadding="0" cellspacing="0" summary="List of users current teams.">
            	<caption>Your Teams</caption>
                <tr>
                	<th>Team</th>
                    <th>League</th>
                    <th>Division</th>
                    <th>Site</th>
                </tr>
                <?php
				
					foreach($db->query("SELECT * FROM teams WHERE user_id = " . $user_id) as $t) {
				
				?>
                <tr id="<?php echo $t['team_id']; ?>">
                	<td>
                    	<img class="logo" src="team_images/<?php echo $t['logo']; ?>" alt="<?php echo $t['city'] . ' ' . $t['name']; ?>" />
                        <span class="city"><?php echo $t['city']; ?></span>
                        <span class="team"><?php echo $t['name']; ?></span>
                        <a href="edit-team/<?php echo $t['team_id']; ?>">Edit</a> | <a href="delete-team/<?php echo $t['team_id']; ?>" class="delete">Delete</a>
                    </td>
                    <td><?php echo $t['league']; ?></td>
                    <td><?php echo $t['division']; ?></td>
                    <td><a href="<?php echo $t['url']; ?>"><?php echo $t['url']; ?></a></td>
                </tr>
                <?php } ?>
            </table>
        </article>
    </section>
    <?php require_once 'include/footer.html'; ?>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
    <script>
    	$(document).ready(function() {
			var t_id;
			$(".delete").click(function() {
				// show delete dialog box
				$("#dialog-confirm").dialog("open");
				t_id = $(this).attr("href");
				t_id = t_id.split("/");
				
				return false;
			});
			
			$("#dialog-confirm").dialog({
				resizable: false,
				draggable: false,
				autoOpen: false,
				modal: true,
				buttons: {
					"Delete Team": function() {
						// delete team via ajax
						$.post(
							"delete-team.php",
							{
								id: t_id[1],
								delete_team: true
							},
							function(data) {
								$(".error").remove();
								$(".success").remove();
								$(".notice").remove();
								$("#dialog-confirm").prepend(data);
								if($(data).hasClass('notice')) {
									// close window after 2.5 seconds on success
									setTimeout(function() { 
										$("#dialog-confirm").dialog("close");
									}, 2500);
									// remove deleted team from table
									$("#" + t_id[1]).fadeOut('slow');
								}
							}
						);
					},
					Cancel: function() {
						$(this).dialog("close");
					}
				}
			});
		});
    </script>
    <div id="dialog-confirm" title="Delete Team">
    	<p>Do you really wish to delete this team?</p>
        <p>Doing so will remove this team &amp; release all associated players and rosters.</p>
    </div>
</body>
</html>